package com.healthcare.admin.interceptor;


import com.healthcare.admin.utils.JwtUtil;
import com.healthcare.common.constants.ResultCodeEnum;
import com.healthcare.common.exception.KetaiException;
import io.jsonwebtoken.Claims;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：LSH
 * 日期：2020/1/13 17:45
 * 拦截用户得请求信息 ，自定义
 */
public class JwtInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
         // 先接收请求中得token信息， 使用request获取header 头部封装得tokent信息
        String authHeader = request.getHeader("X-Token");

        System.out.println("截取到request请求中得token值："+authHeader);
        if(authHeader == null){  //如果请求中得token没值，则默认为没登陆
            throw  new KetaiException(ResultCodeEnum.USERLOGIN_ERROR);
        }
        // 验证token
        Claims claims = JwtUtil.checkToken(authHeader);
        return true;
    }
}
